﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
namespace URIDecoder
{
    class Program
    {
        static void WriteError(string s)
        {
            ConsoleColor? cc = null;
            try
            {
                cc = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Red;
            }
            catch (Exception)
            {
                cc = null;
            }
            Console.Error.WriteLine(s);
            if (cc != null) Console.ForegroundColor = cc.Value;
        }
        [STAThread]
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("URIDecoder by @Shyc2001");
                Console.WriteLine("命令行参数说明：");
                Console.WriteLine("URIDecoder.exe uri\t解码指定的URI并使用设置文件中的默认下载程序打开。");
                Console.WriteLine("或者：");
                Console.WriteLine("URIDecoder.exe uri /clip\t解码指定的URI并复制到剪贴板。（仅 Windows ）");
                Console.WriteLine("uri - 要解码的uri");
                return;
            }
            ConfigFile cf = ConfigFile.FromFile();
            
            string s = args[0];
            try
            {
                s = cf.AutoDecode(s);
                if (s == null) throw new ApplicationException("URI不符合对应协议的规则！");
            }
            catch (Exception ex)
            {
                WriteError("下载地址有误！" + Environment.NewLine + ex.Message);
                Console.WriteLine("请按任意键退出……");
                Console.ReadKey();
                return;
            }
            if (args.Length > 1)
            {
                if (args[1] == "/clip")
                {
                    string fullname = "System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
                    try
                    {
                        System.Reflection.Assembly.Load(fullname)
                            .GetType("System.Windows.Forms.Clipboard")
                            .InvokeMember("SetText", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.InvokeMethod
                            , null, null, new object[] { s });
                        Console.WriteLine("URI 已经复制到剪贴板。");
                        return;
                    }
                    catch (FileNotFoundException)
                    {

                    }
                    catch (BadImageFormatException)
                    {

                    }
                    catch (FileLoadException)
                    {

                    }
                    catch(Exception ex)
                    {
                        WriteError("复制 URI 到剪贴板时失败！" + Environment.NewLine + ex.Message);
                        Console.WriteLine("请按任意键退出……");
                        Console.ReadKey();
                    }
                    WriteError("加载程序集 System.Windows.Forms 时失败！");
                    WriteError("您必须在 Windows 操作系统上安装 .NET Framework 2.0 才能使用剪贴板功能。");
                    WriteError("（暂时不支持 .NET Framework 的更高版本！）");
                    Console.WriteLine("请按任意键退出……");
                    Console.ReadKey();
                    return;
                }
            }
            try
            {
                cf.Download(s);
            }
            catch (Exception ex)
            {
                WriteError("启动下载程序失败！" + Environment.NewLine + ex.Message);
                Console.WriteLine("请按任意键退出……");
                Console.ReadKey();
                return;
            }
        }
    }
    
}
